package com.polytech.ihm.factory;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.util.HashMap;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

public class ButtonsPanel extends JPanel {
 
	private JPanel pan=new JPanel();
	public final static int VERTICAL=0;
	public final static int HORIZONTAL=1;
	private HashMap<String, JButton>buttons=new HashMap<>();
	public ButtonsPanel(String caption[]) {
		int MAX_WIDTH=0;
		setLayout(new FlowLayout(FlowLayout.RIGHT));
		for (int i = 0; i < caption.length; i++) {
			JButton bt=new JButton(caption[i]);
			if(bt.getPreferredSize().width>MAX_WIDTH){
				MAX_WIDTH=bt.getPreferredSize().width;
			}
			pan.add(bt);
			buttons.put(caption[i], bt);
		}
		for (int i = 0; i < caption.length; i++) {
			JButton bt=(JButton)pan.getComponent(i);
			bt.setPreferredSize(new Dimension(MAX_WIDTH,bt.getPreferredSize().height));
		}
		add(pan);
	}
	public ButtonsPanel(String caption[],int alignement) {
	   this(caption);
	   if(alignement==VERTICAL){
		   pan.setLayout(new GridLayout(caption.length,1,5,5));
	   }
	}
	public void addActionListner(String id,ActionListener action){
		JButton bt=buttons.get(id);
		bt.addActionListener(action);
	}
}
